#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'Eto Demerzel'

import sqlite3
import sys
import os

def create_table(db_name, nodes,edges):
    db_file_name=db_name

    if os.path.isfile(db_file_name):
        os.remove(db_file_name)

    db = sqlite3.connect(db_file_name)

    db.text_factory = str
    cur = db.cursor()

    cur.executescript("""
        create table nodes(
            id,
            leftkey,
            rightkey,
            label,
            size
        );

        create table edges(
            source,
            target
        );
        """)

    c = db.cursor()

    #nodes=nodes.items()
    #print nodes
    #print edges
    for i in xrange(len(nodes)):
        c.execute('insert into nodes values (?,?,?,?,?)', (str(nodes[i][0]),str(nodes[i][1]),str(nodes[i][2]), str(i),1))

    #edges=edges.items()

    for i in xrange(len(edges)):
        c.execute('insert into edges values (?,?)', (str(edges[i][0]),str(edges[i][1])))
    db.commit()
    print "database %s was created successfully..." %db_name
    c.close()


def main():
    create_table(nodes,edges)

if __name__ == "__main__":
    sys.exit(main())